/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TraCuuChuyenBay.java
 *
 * Created on May 28, 2011, 10:49:09 AM
 */
package gui;

import dao.ChuyenBayDAO;
import dao.SanBayDAO;
import dao.TuyenBayDAO;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.ChuyenBayPOJO;
import pojo.SanBayPOJO;
import pojo.TuyenBayPOJO;

/**
 *
 * @author b2
 */
public class TraCuuChuyenBay extends javax.swing.JFrame {

    /** Creates new form TraCuuChuyenBay */
    private String[] dsSanBayDi;
    private String[] dsSanBayDen;
    private String maTuyenBay;
    ArrayList<SanBayPOJO> dsSanBay;

    public TraCuuChuyenBay() {
        initComponents();
    }

    public void initTraCuuChuyenBay() {
        dsSanBay = SanBayDAO.LayDanhSachSanBay();

        dsSanBayDi = new String[dsSanBay.size()];
        dsSanBayDen = new String[dsSanBay.size()];

        for (int i = 0; i < dsSanBay.size(); i++) {
            SanBayPOJO tb = dsSanBay.get(i);
            dsSanBayDen[i] = tb.getTenSanBay();
            dsSanBayDi[i] = tb.getMaSanBay();
        }
        DefaultComboBoxModel sbdi = new DefaultComboBoxModel(dsSanBayDi);
        this.jcbSanBayDi.setModel(sbdi);
        this.chonTuyenBay();
        this.hienthiTenSanBay();
        Calendar clendar = new GregorianCalendar();
        Date curDate = clendar.getTime();
        this.jdcDenNgay.setDate(curDate);
        this.jdcTuNgay.setDate(curDate);

    }

    public void hienthiTenSanBay() {
        String maSanBayDi = (String) this.jcbSanBayDi.getSelectedItem();
        String maSanBayDen = (String) this.jcbSanBayDen.getSelectedItem();
        SanBayPOJO sbDi = SanBayDAO.laySanBay(maSanBayDi);
        SanBayPOJO sbDe = SanBayDAO.laySanBay(maSanBayDen);
        this.jlbSanBayDen.setText(sbDe.getTenSanBay());
        this.jlbSanBayDi.setText(sbDi.getTenSanBay());
    }
    public void chonTuyenBay(){
        int index = this.jcbSanBayDi.getSelectedIndex();
        String maSB = dsSanBay.get(index).getMaSanBay();
        ArrayList<TuyenBayPOJO> ds = TuyenBayDAO.LayTuyenBayDen(maSB);

        String[] dsSBDen = new String[ds.size()];

        for (int i = 0; i < ds.size(); i++) {
            TuyenBayPOJO tb = ds.get(i);
            dsSBDen[i] = tb.getSanBayDen();
        }
        DefaultComboBoxModel sbde = new DefaultComboBoxModel(dsSBDen);
        this.jcbSanBayDen.setModel(sbde);
        this.hienthiTenSanBay();
        int inde = this.jcbSanBayDen.getSelectedIndex();
        maTuyenBay = maSB + "-" + dsSBDen[inde];
    
    }
    public void timKiemChuyenBay() {

        
        String patten = "MM/dd/yyyy";
        SimpleDateFormat sdf = new SimpleDateFormat(patten);
        //------
        Date tuNgay = this.jdcTuNgay.getDate();
        String stuNgay = sdf.format(tuNgay);
        //-----------
        Date denNgay = this.jdcDenNgay.getDate();
        String stenNgay = sdf.format(denNgay);
        //---------------
        JOptionPane.showMessageDialog(this, stenNgay +"......" + stenNgay);
        ArrayList<ChuyenBayPOJO> ds = ChuyenBayDAO.TimKiemChuyenBayTheoTB(maTuyenBay); 
        //ArrayList<ChuyenBayPOJO> ds = ChuyenBayDAO.TimKiemChuyenBay(stuNgay,stenNgay);
        //ArrayList<ChuyenBayPOJO> ds = ChuyenBayDAO.TimKiemChuyenBay(tuNgay,denNgay);
        
            DefaultTableModel dtm = new DefaultTableModel();
          dtm.setNumRows(ds.size());
        for (int i = 0; i < ds.size(); i++) {
            ChuyenBayPOJO cb = ds.get(i);
            dtm.setValueAt(cb.getMaChuyenBay(), i, 0);
            dtm.setValueAt(cb.getMaTuyenBay(), i, 1);
            dtm.setValueAt(cb.getNgayGio(), i, 2);
            dtm.setValueAt(cb.getThoiGianBay(), i, 3);
            dtm.setValueAt(cb.getSLG1(), i, 4);
            dtm.setValueAt(cb.getSLG1(), i, 5);

        }
        this.jtbDanhSachChuyenBay.setModel(dtm);
        this.jtbDanhSachChuyenBay.changeSelection(0, 0, false, false);
        

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jcbSanBayDi = new javax.swing.JComboBox();
        jcbSanBayDen = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jdcTuNgay = new com.toedter.calendar.JDateChooser();
        jLabel6 = new javax.swing.JLabel();
        jdcDenNgay = new com.toedter.calendar.JDateChooser();
        jlbSanBayDi = new javax.swing.JLabel();
        jlbSanBayDen = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtbDanhSachChuyenBay = new javax.swing.JTable();
        btnTimKiem = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Tra cứu chuyến bay");
        setBounds(new java.awt.Rectangle(200, 200, 0, 0));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setText("TRA CỨU CHUYẾN BAY");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Tra cứu chuyến bay"));

        jLabel2.setText("Sân bay đi");

        jLabel3.setText("Sân bay đến");

        jcbSanBayDi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbSanBayDiActionPerformed(evt);
            }
        });

        jcbSanBayDen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbSanBayDenActionPerformed(evt);
            }
        });

        jLabel4.setText("Ngày khởi hành");

        jLabel5.setText("Từ ngày");

        jLabel6.setText("Đến ngày");

        jtbDanhSachChuyenBay.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(jtbDanhSachChuyenBay);

        btnTimKiem.setText("Tìm Kiếm");
        btnTimKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTimKiemActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(166, 166, 166)
                                .addComponent(btnTimKiem))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(95, 95, 95)
                                .addComponent(jdcTuNgay, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(134, 134, 134)
                                .addComponent(jdcDenNgay, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(46, 46, 46)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel2)
                                        .addComponent(jLabel3)
                                        .addComponent(jLabel5))
                                    .addGap(45, 45, 45)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jcbSanBayDi, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jcbSanBayDen, 0, 100, Short.MAX_VALUE))
                                    .addGap(56, 56, 56)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jlbSanBayDen, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel6)
                                        .addComponent(jlbSanBayDi, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE))
                                .addComponent(jLabel4)))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                            .addGap(33, 33, 33)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(33, Short.MAX_VALUE))
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 518, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jcbSanBayDi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlbSanBayDi))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jcbSanBayDen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlbSanBayDen))
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jdcDenNgay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jdcTuNgay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5)))
                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(40, 40, 40)
                .addComponent(btnTimKiem)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(141, 141, 141)
                        .addComponent(jLabel1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        this.initTraCuuChuyenBay();
    }//GEN-LAST:event_formWindowOpened

    private void jcbSanBayDiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbSanBayDiActionPerformed
        // TODO add your handling code here:
        this.chonTuyenBay();
        

    }//GEN-LAST:event_jcbSanBayDiActionPerformed

    private void btnTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTimKiemActionPerformed
        // TODO add your handling code here:
        this.timKiemChuyenBay();
    }//GEN-LAST:event_btnTimKiemActionPerformed

    private void jcbSanBayDenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbSanBayDenActionPerformed
        // TODO add your handling code here:
        this.hienthiTenSanBay();
    }//GEN-LAST:event_jcbSanBayDenActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TraCuuChuyenBay().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnTimKiem;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    private javax.swing.JComboBox jcbSanBayDen;
    private javax.swing.JComboBox jcbSanBayDi;
    private com.toedter.calendar.JDateChooser jdcDenNgay;
    private com.toedter.calendar.JDateChooser jdcTuNgay;
    private javax.swing.JLabel jlbSanBayDen;
    private javax.swing.JLabel jlbSanBayDi;
    private javax.swing.JTable jtbDanhSachChuyenBay;
    // End of variables declaration//GEN-END:variables
}
